/*
 * @Author: Wangjun
 * @Date: 2023-08-14 10:40:47
 * @LastEditTime: 2023-12-02 09:45:54
 * @LastEditors: wangjun haodreams@163.com
 * @Description:
 * @FilePath: \xr_node_calc\noderdb\op_magus_di.go
 * hnxr
 */
package noderdb

import (
	"errors"

	"gitee.com/haodreams/godriver/opdb"
	"gitee.com/hnxr_1/nodex"
)

func init() {
	nodex.Reister(new(MagusDI))
}

type MagusDI struct {
	nodex.Node
	Y    nodex.Boolean `remark:"输出Y,,out"`
	item *opdb.Real
	Tag  string
	Kind string //类别
}

func (m *MagusDI) GetModelID() int {
	return nodex.ModIn
}

func (m *MagusDI) GetName() string {
	return "MagusDI"
}

func (m *MagusDI) SetExtends(ptr map[string]any) {
	m.Node.SetExtends(ptr)
}

func (m *MagusDI) Init(mem *nodex.Memory) (err error) {
	extends := m.GetExtends()
	atag := extends["tag"]
	tag, _ := atag.(string)
	acategory := extends["category"]
	category, _ := acategory.(string)
	if tag != "" {
		m.Tag = tag
		m.Kind = category
		m.item = dataitem.GetOPValue(tag, true)
	}

	return
}

/**
 * @description: 节点信息
 * @return {*}
 */
func (m *MagusDI) RegData() []byte {
	return nodex.MakeRegData(m, "麦杰实时库开关量输入", m.GetName(), "")
}

func (m *MagusDI) Calc() (err error) {
	// parent := m.GetParent()
	// if m.Parent.RID == 316 {
	// 	log.Println(m.Tag, m.item.V, m.item.Q)
	// }
	if m.item == nil {
		m.Bad = true
		m.Err = errors.New(m.Tag + ":测点不存在")
		if m.EnQ {
			return m.Err
		}
		return
	}
	if m.item.Status < 0 {
		m.Err = errors.New(m.Tag + ":测点不存在")
		m.Bad = true
		if m.EnQ {
			return m.Err
		}
		return
	}
	m.Y.Value = int(m.item.Value) != 0
	m.Bad = false
	//fmt.Printf("MagusDI:%p\n", m.Y.BoolValue)
	return
}
